<?php

/**
 * WidgetPosition form.
 *
 * @package    berta-s
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class WidgetPositionFormForBackend extends WidgetPositionForm{
    private $widgetSettingsInitialized=false;

    public function configure(){
        $this->widgetSchema   ['widget_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Widget'), 'add_empty' => false));
        $this->validatorSchema['widget_id'] = new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Widget'), 'required' => true));

        $this->widgetSchema   ['placeholder_name'] = new sfWidgetFormInputText();
        $this->validatorSchema['placeholder_name'] = new sfValidatorString(array('max_length' => 255, 'required' => true));

        $widgetSlug="";
        $widgetPosition=$this->getObject();
        if (!empty($widgetPosition)){
            $widget=$widgetPosition->getWidget();
            if (!empty($widget)){
                $widgetSlug=$widget->getSlug();
            }
        }

        $this->initWidgetSettings($widgetSlug);

    }


    public function initWidgetSettings($widgetSlug){
        $templateSettings=TemplateSettings::getInstance();
        $templateDirectory=$templateSettings->getDirectoryName();
        $templateFullPath=sfConfig::get('sf_plugins_dir')."/bertaTwigPlugin/templates/{$templateDirectory}";

        $yaml=sfYaml::load("{$templateFullPath}/widgets/{$widgetSlug}-settings.yml");

        FormYamlConfig::configFormFromYaml($this, $yaml);

        if (is_array($yaml) && sizeof($yaml)>0){
            $this->widgetSettingsInitialized=true;
        }
    }



}
